#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include <interface/data_op/table_wrapper.h>
#include "multi_chat_session_op.h"
#include <vector>
#include <algorithm>   

namespace aot{ namespace data{

multi_chat_session_op_impl::multi_chat_session_op_impl(void)
{

}

multi_chat_session_op_impl::~multi_chat_session_op_impl(void)
{

}

bool
multi_chat_session_op_impl::insert_session(const imulti_chat_session_info* p, int* new_id)
{
    if (!g_db_user) return false;
    
    std::string str_member_user_id_list = p->member_user_id_list->c_str();
    std::string str_create_datetime = p->create_datetime->c_str();
    std::string str_last_chat_datetime = p->last_chat_datetime->c_str();

    remove_sin_quot(str_member_user_id_list);
    remove_sin_quot(str_create_datetime);
    remove_sin_quot(str_last_chat_datetime);

    std::string sql_fmt = 

        "insert into chat_multi_session \
        (id,\
        member_user_id_list,\
        create_datetime,\
        last_chat_datetime) \
        values\
        (%d, '%s', '%s', '%s');";

    std::string sql_exe;
    int t_new_id = get_table_next_id("chat_multi_session");

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        t_new_id,
        str_member_user_id_list.c_str(),
        str_create_datetime.c_str(),
        str_last_chat_datetime.c_str()
        );

    *new_id = t_new_id;
    return g_db_user->exec(sql_exe.c_str());
}

bool
multi_chat_session_op_impl::delete_session(int id)
{
    std::string sql_fmt = "delete from chat_multi_session where id=%d";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id);
    return g_db_user->exec(sql_exe.c_str());
}

bool
multi_chat_session_op_impl::delete_session(const char* id_list)
{
    std::string sql_fmt = "delete from chat_multi_session where member_user_id_list='%s'";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id_list);
    return g_db_user->exec(sql_exe.c_str());
}

bool
multi_chat_session_op_impl::update_session(const imulti_chat_session_info* p)
{
    if (!g_db_user) return false;

    std::string str_member_user_id_list = p->member_user_id_list->c_str();
    std::string str_create_datetime = p->create_datetime->c_str();
    std::string str_last_chat_datetime = p->last_chat_datetime->c_str();

    remove_sin_quot(str_member_user_id_list);
    remove_sin_quot(str_create_datetime);
    remove_sin_quot(str_last_chat_datetime);

    std::string sql_fmt = 
        "update chat_multi_session set \
        member_user_id_list='%s',\
        create_datetime='%s',\
        last_chat_datetime='%s' \
        where id =%d";

    std::string sql_exe;

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        str_member_user_id_list.c_str(),
        str_create_datetime.c_str(),
        str_last_chat_datetime.c_str()
        );

    return g_db_user->exec(sql_exe.c_str());
}

bool
multi_chat_session_op_impl::is_session_exist(const char* id_list)
{
    int total;
    std::string sql_fmt = "select count(*) as T from chat_multi_session where member_user_id_list='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id_list);
    total = g_db_user->exec_scalar(sql_exe.c_str());
    return (total>0);
}

bool
multi_chat_session_op_impl::read_session(int id, isqlite3_table** out)
{
    std::string sql_fmt = "select id,\
                            member_user_id_list,\
                            create_datetime,\
                            last_chat_datetime \
                            from chat_multi_session where id=%d";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool
multi_chat_session_op_impl::read_session(int id, imulti_chat_session_info* out)
{
    aot::data::table_helper t_table;
    bool ret =false;
    if (this->read_session(id,t_table))
    {
        if (t_table.get_rows_count() > 0)
        {
            t_table.set_row_index(0);

            out->id = t_table.get_field_value_int("id");
            out->member_user_id_list->assign(t_table.get_field_value_str("member_user_id_list").c_str());
            out->create_datetime->assign(t_table.get_field_value_str("create_datetime").c_str());
            out->last_chat_datetime->assign(t_table.get_field_value_str("last_chat_datetime").c_str());

            ret = true;
        }
    }
    return ret;
}

bool
multi_chat_session_op_impl::read_session(int id, imulti_chat_session_info** out)
{
    aot::data::imulti_chat_session_info* obj = new aot::data::multi_chat_session_info_impl();
    *out = obj;
    return this->read_session(id, obj);
}

bool
multi_chat_session_op_impl::read_session(const char* id_list, isqlite3_table** out)
{
    std::string sql_fmt = "select id,\
                          member_user_id_list,\
                          create_datetime,\
                          last_chat_datetime \
                          from chat_multi_session where member_user_id_list='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id_list);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool
multi_chat_session_op_impl::read_session(const char* id_list, imulti_chat_session_info* out)
{
    aot::data::table_helper t_table;
    bool ret =false;
    if (this->read_session(id_list, t_table))
    {
        if (t_table.get_rows_count() > 0)
        {
            t_table.set_row_index(0);

            out->id = t_table.get_field_value_int("id");
            out->member_user_id_list->assign(t_table.get_field_value_str("member_user_id_list").c_str());
            out->create_datetime->assign(t_table.get_field_value_str("create_datetime").c_str());
            out->last_chat_datetime->assign(t_table.get_field_value_str("last_chat_datetime").c_str());

            ret = true;
        }
    }
    return ret;
}

bool
multi_chat_session_op_impl::read_session(const char* id_list, imulti_chat_session_info** out)
{
    aot::data::imulti_chat_session_info* obj = new aot::data::multi_chat_session_info_impl();
    *out = obj;
    return this->read_session(id_list, obj);
}

bool
multi_chat_session_op_impl::read_all_sessions(isqlite3_table** out)
{
    std::string sql_fmt = "select id,\
                          member_user_id_list,\
                          create_datetime,\
                          last_chat_datetime \
                          from chat_multi_session";
    std::string sql_exe = sql_fmt;
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool
multi_chat_session_op_impl::get_all_id_list(aot::tt::ivec_str** out)
{
    aot::tt::vec_str_impl* p = new aot::tt::vec_str_impl();
    *out = (aot::tt::ivec_str*)p;

    std::string sql_exe;
    sql_exe = "select member_user_id_list from chat_multi_session order by last_chat_datetime desc;";
    aot::data::table_helper t_table;

    bool ret = g_db_user->exec_and_get_record(sql_exe.c_str(), t_table);

    std::string value;
    for( int i = 0; i < t_table.get_rows_count(); i++ )
    {
        t_table.set_row_index(i);

        value = t_table.get_field_value_str("chat_multi_session");
        p->impl().push_back(value);
    }

    return ret;
}

bool
multi_chat_session_op_impl::get_id_list_string(aot::tt::ivec_str* lst, aot::tt::istr** out)
{
    aot::tt::str_impl* impl = new aot::tt::str_impl();
    *out = impl;
    
    std::vector<std::string> temp_list;
    
    for(lst->goto_begin(); !lst->is_end(); lst->goto_next())
    {
        temp_list.push_back(lst->get_value());
    }
    
    sort(temp_list.begin(), temp_list.end());     

    std::vector<std::string>::const_iterator it = temp_list.begin();
    for ( ; it != temp_list.end(); it++ )
    {
        if ( it == temp_list.begin() )
        {
            impl->str_ = *it;
        }
        else
        {
            impl->str_ = impl->str_ + "," + (*it);
        }
    }
    return true;
}

}} /* end namespace aot/data*/